import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_250303/datas/home_banner_data.dart';
import 'package:flutter_250303/datas/home_list_data.dart';
import 'package:flutter_250303/http/api.dart';
import 'package:flutter_250303/http/dio_instance.dart';

class HomeViewModel with ChangeNotifier {
  List<BannerItemData>? bannerList;
  List<HomeListItemData>? listData;
  int _pageCount = 0;

  // Dio dio = Dio();

  // void initDio() {
  //   dio.options = BaseOptions(
  //       method: "GET",
  //       baseUrl: "https://www.wanandroid.com/",
  //       connectTimeout: Duration(seconds: 30),
  //       receiveTimeout: Duration(seconds: 30),
  //       sendTimeout: Duration(seconds: 30));
  // }

  // Future getBanner() async {
  //   Response response = await DioInstance.instance().get(path: "banner/json");
  //   // Map<String, dynamic> map = jsonDecode(response.data);
  //   HomeBannerData bannerData = HomeBannerData.fromJson(response.data);
  //   if (bannerData != null && bannerData.bannerList != null) {
  //     bannerList = bannerData.bannerList;
  //   } else {
  //     bannerList = [];
  //   }
  //   notifyListeners();
  // }

  Future getBanner() async {
    List<BannerItemData>? list = await Api.instance.getBanner();
    bannerList = list ?? [];
    notifyListeners();
  }

  // Future getHomeList() async {
  //   Response response = await DioInstance.instance().get(path: "article/list/0/json");
  //   // Map<String, dynamic> map = jsonDecode(response.data);
  //   HomeListData homeData = HomeListData.fromJson(response.data);
  //   if (homeData != null && homeData.datas != null) {
  //     listData = homeData.datas;
  //   } else {
  //     listData = [];
  //   }
  //   notifyListeners();
  // }

  Future getHomeList(bool loadMore) async {
    if (loadMore) {
      _pageCount++;
    } else {
      _pageCount = 0;
      listData = [];
    }
    print("===sf===> $_pageCount");
    HomeListData? list = await Api.instance.getHomeList("$_pageCount");
    if ((list?.datas ?? []).isEmpty && _pageCount > 0) {
      _pageCount--;
    }
    listData?.addAll(list?.datas ?? <HomeListItemData>[]);
    notifyListeners();
  }
}
